﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace Serwer
{
    public partial class Serwer : Form
    {
        public Serwer()
        {
            InitializeComponent();

            //IPAddress ip = IPAddress.Parse("192.168.0.2");     //adres serwera
            //TcpListener serwer = new TcpListener(ip, 2000);  //tworzymy obietk serwer 
            //serwer.Start();
            //while (true)
            //{
            //    TcpClient client = serwer.AcceptTcpClient(); //akceptujemy żądanie połączenia
            //    NetworkStream nstream = client.GetStream();  //pobieramy strumień do wymiany danych
            //    StreamWriter streamw = new StreamWriter(nstream);
            //    streamw.WriteLine("Witamy na serwerze komunikatora"); //wysyłamy informacje 
            //    streamw.Flush();
            //}
        }

        private void Serwer_Load(object sender, EventArgs e)
        {
            try
            {
                IPAddress ipAd = IPAddress.Parse("192.168.0.2");
                TcpListener myList = new TcpListener(ipAd, 8001);
                myList.Start();
                Socket s = myList.AcceptSocket();
                MessageBox.Show("Connection accepted from " + s.RemoteEndPoint);
                byte[] b = new byte[100];
                int k = s.Receive(b);
                MessageBox.Show("Recieved...");
                string message = "" ;
                for (int i = 0; i < k; i++)
                    message += (Convert.ToChar(b[i]));
                MessageBox.Show(message);

                ASCIIEncoding asen = new ASCIIEncoding();
                s.Send(asen.GetBytes("The string was recieved by the server."));
                MessageBox.Show("\nSent Acknowledgement");
                /* clean up */
                s.Close();
                myList.Stop();

            }
            catch (Exception ex)
            {
                Console.WriteLine("Error..... " + ex.StackTrace);
            }
        }
    }
}
